package pos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import pos.domain.Payment;
import pos.domain.SaleMade;
import pos.utils.UIDGenerator;

public class JDBCPaymentDAO implements IPaymentDAO {
	private Connection conn;
	private ISaleMadeDAO smdao;
	
	public JDBCPaymentDAO () {
		this.conn = ConnectionManager.getInstance().checkOut();
		smdao = new JDBCSaleMadeDAO();
	}
	
	@Override
	public void deletePayment(String payid) {
		String sql = "DELETE FROM payments WHERE (paymentID = ?)";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, payid);
			stmt.executeUpdate();

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}
		}
	}

	@Override
	public void insertPayment(Payment pay) {
		String oido = UIDGenerator.getInstance().getKey();
        PreparedStatement stmt = null;
        String sql = "INSERT INTO payments (oid, paymentsID, name) VALUES (?, ?, ?)";
       
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, oido);
            stmt.setString(2, pay.getPaymentID());
            stmt.setString(3, pay.getName());

            stmt.executeUpdate();
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
	}

	@Override
	public Payment selectPaymentBySaleMade(String saleMadeid) {
		SaleMade venta = smdao.selectSaleMade(saleMadeid);
		String paymentID = venta.getPaymentID();
		return selectPayment(paymentID);
	}

	public Payment selectPayment(String paymentID) {
		Payment res = null;
		PreparedStatement stmt = null;
		ResultSet result = null;
		String sql = "SELECT FORM payments WHERE (paymentID = ?)";
		
		try{
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, paymentID);
			result = stmt.executeQuery();
			
			result.next();
			res = new Payment();
			res.setPaymentID(result.getString("paymentID"));
			res.setName(result.getString("name"));
			
		} catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		return res;
	}

}
